package org.bim.view.drag.design.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bim.view.drag.design.api.dto.LoginDTO;
import org.bim.view.drag.design.api.vo.TUserVO;
import org.bim.view.drag.design.config.DesignerLoginService;
import org.bim.view.drag.design.entity.TUser;
import org.bim.view.drag.design.model.HttpResult;
import org.bim.view.drag.design.util.ThreadLocalUtil;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api
@RestController
@RequiredArgsConstructor
@RequestMapping
public class LoginController {
    private final DesignerLoginService designerLoginService;
    @PostMapping("login")
    public HttpResult<TUserVO> login(@RequestBody LoginDTO dto){
        TUserVO tUserVO =designerLoginService.login(dto);
        return HttpResult.success(tUserVO);
    }
    @PostMapping("register")
    public HttpResult register(@RequestBody TUser dto){
        designerLoginService.register(dto);
        return HttpResult.success();
    }
    @GetMapping("check-token")
    public HttpResult<Boolean> checkToken(@ApiParam(required = true) @RequestParam String token){
       boolean flag =  designerLoginService.checkToken(token);
       if(flag){
           return HttpResult.success(true);
       }
       return HttpResult.fail(400,"token无效");

    }
    @GetMapping("user/info")
    public HttpResult<TUserVO> getUserInfo(){
        TUserVO now =  ThreadLocalUtil.getCurrentUser();
        return HttpResult.success(now);
    }
    @PostMapping("logout")
    public HttpResult logout(){
        TUserVO now =  ThreadLocalUtil.getCurrentUser();
        designerLoginService.logout(now.getToken());
        return HttpResult.success();
    }
}
